package cityrolling.game;

import static cityrolling.game.Constant.SCREEN_HEIGHT;
import static cityrolling.game.Constant.SCREEN_WIDTH;

import java.io.InputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import cityrolling.ToolModule.SimpleToolController;
import cityrolling.ToolModule.ToolController;
import cityrolling.ToolModule.ToolEffectSet;
import data.GameMap;

public class SimpleGameView extends GLSurfaceView implements Callback, GameView{
	ToolController tool;
	CityRollingActivity activity;
	Bitmap back;
	Ball ball;
	SurfaceHolder holder;
	
	Canvas c;
	public static boolean game=true;      //playing
	public static int guankaID;//绠�?崱ID
	//int count=0;
	public SimpleGameView(CityRollingActivity activity) {
		super(activity);
		getHolder().addCallback(this);
		this.activity = activity;
		init();
	}
	
	private void init() {
		initMap();
		back = BitmapFactory.decodeResource(activity.getResources(), R.drawable.bg);
		ball = new Ball(100, 100, this);
		tool = createToolController();
	}
	
	private void initMap() {
		GameMap gMap = new GameMap();
		InputStream is = getResources().openRawResource(R.raw.data);
		gMap.loadMapData(is);
	}
	
	public void onDraw() {
		super.onDraw(c);
		c.drawColor(Color.argb(255, 0, 0, 0));
		c.drawBitmap(back,30,0, null); //draw background
		ball.onDraw(c);
		tool.drawTools(c);
	}
	
	public boolean onTouchEvent(MotionEvent event) {
		int x = (int) event.getX();
		int y = (int) event.getY();
		if (x > 0 && x < SCREEN_WIDTH && y > 0 && y < SCREEN_HEIGHT ){
			//do nothing
		}
		return super.onTouchEvent(event);
	}
	
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
	}
	
	public void surfaceCreated(SurfaceHolder holder) {
		this.holder=holder;
		new Thread(new SimpleGameViewThread(this)).start();   
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
	}

	public void createGameView() {
		// TODO Auto-generated method stub
		this.tool = createToolController();
		
	}
	public ToolController createToolController() {
		// TODO Auto-generated method stub
		ToolEffectSet tes = new ToolEffectSet();
		tes.setBall(ball);
		tes.setGameActivity(activity);
		return new SimpleToolController(tes);
	}
	
	class SimpleGameViewThread extends Thread{
		SimpleGameView s;
		
		public SimpleGameViewThread(SimpleGameView simpleGameViewThread){
			this.s = simpleGameViewThread;
		}
		
		public void run(){
			c = null;
			while (game) {
				try {
					c = holder.lockCanvas(null);
					synchronized (holder) {
						onDraw(c);
						c.drawColor(Color.argb(255, 0, 0, 0));
						c.drawBitmap(back,30,0, null); //draw background
						ball.onDraw(c);
						tool.drawTools(c);
						try {
							sleep(10);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				} finally {
					if (c != null) {
						holder.unlockCanvasAndPost(c);
					}
				}
			}
		}
	}

}
